<script>
	import {updateUrl} from "@/common/config.js"
	export default {
		onLaunch: function() {
			console.log('App Launch')
			//#ifdef APP-PLUS  
			const updated = uni.getStorageSync('updated'); // 尝试读取storage
			if (uni.getStorageSync('updated_version')) {
				var version = uni.getStorageSync('updated_version');
				//更新后版本号存储未更新，那么重新存储
				if(version < plus.runtime.version){
					version = plus.runtime.version;
					uni.setStorageSync('updated_version', version);
				}
				
			} else {
				//存储初始版本号
				var version = plus.runtime.version;
				uni.setStorageSync('updated_version', version);
			}


			var server =updateUrl ; //检查更新地址
			var req = { //升级检测数据  
				"appid": plus.runtime.appid,
				"version": version,
				"name": plus.os.name
			};
			// var req = { //升级检测数据
			// 	"appid": 123,
			// 	"version": 123,
			// 	"name": "Android"
			// };
			uni.request({
				url: server,
				data: req,
				method: "post",
				header: {
					'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
				},
				success: (res) => {
					console.log(res)
					// 整包更新 
					var rsp = res.data.data.rsp;
					if (res.statusCode == 200 && rsp.status === 1) {
						uni.showModal({ //提醒用户更新  
							title: "更新提示",
							content: rsp.note,
							success: (res) => {
								if (res.confirm) {
									uni.setStorageSync('updated_version', '');
									plus.runtime.openURL(rsp.url);
								}
							}
						})
					}

					// 热更新
					if (res.statusCode == 200 && rsp.status === 2) {
						uni.showModal({ //提醒用户更新  
							title: "发现新版本",
							content: rsp.note,
							showCancel: false,
							confirmText: "立即下载更新",
							success: (res) => {
								if (res.confirm) {
									var w = plus.nativeUI.showWaiting("开始下载...");
									uni.downloadFile({
										url: rsp.url,
										success: (downloadResult) => {
											if (downloadResult.statusCode ===
												200) {
												plus.runtime.install(downloadResult
													.tempFilePath, {
														force: false
													},
													function() {
														console.log(
															'install success...'
														);
														plus.nativeUI
															.closeWaiting();
														plus.nativeUI.alert(
															"更新完成！",
															function() {

																uni.setStorageSync(
																	'updated_version',
																	rsp
																	.to_version
																	);
																plus.runtime
																	.restart();
															});
													},
													function(e) {
														console.error(
															'install fail...'
														);
														plus.nativeUI
															.closeWaiting();
														plus.nativeUI.alert(
															"更新失败[" + e
															.code + "]：" +
															e.message);
													});


											}
										}
									});
								}
							}
						})
					}


				}
			})
			//#endif 
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
	//全局样式
	@import "./static/style/global.css"
</style>
